package com.angenela.imageshare.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.angenela.imageshare.R;
import com.angenela.imageshare.dto.TagDTO;
import com.angenela.imageshare.pojo.UserAndImage;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater layoutInflater;
    private List<UserAndImage> userAndImageList;
    List<Integer> userStarInfo;
    private int count=0;

    public ImageAdapter(Context context , List<UserAndImage> userAndImageList,  List<Integer> userStarInfo){
        this.userAndImageList = userAndImageList;
        this.mContext = context;
        this.userStarInfo = userStarInfo;
        this.count = userAndImageList.size();
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return this.count;
    }

    @Override
    public Object getItem(int position) {
        return userAndImageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder{
        public ImageView imageView;
        public TextView tv_imageId;
        public TextView username;
        public ImageView avatar;
        public TextView tv_start;
        public ImageView iv_start;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserAndImage contact = userAndImageList.get(position);

        ViewHolder holder;
        if(convertView == null) {
            // 将模板加载为程序中的变量
            convertView = layoutInflater.inflate(R.layout.grid_item, null);
            // 创建新的holder
            holder = new ViewHolder();
            // 通过模板对象获取各个具体显示数据的控件对象
            holder.imageView = convertView.findViewById(R.id.iv_grid);
            holder.tv_imageId = convertView.findViewById(R.id.tv_imageId);
            holder.username = convertView.findViewById(R.id.tv_username);
            holder.avatar = convertView.findViewById(R.id.iv_avatar);
            holder.iv_start = convertView.findViewById(R.id.iv_star);
            holder.tv_start = convertView.findViewById(R.id.tv_star);

            // 将holder封装到convertView
            convertView.setTag(holder);
        } else {
            // 从convertView中获取holder
            holder = (ViewHolder) convertView.getTag();
        }

        holder.username.setText(contact.getUsername());
        holder.tv_start.setText(String.valueOf(contact.getStar()));
        holder.tv_imageId.setText(String.valueOf(contact.getImageId()));
        holder.iv_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    onItemStar.onItemStar(position);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

        Glide.with(mContext).load(contact.getSrc()).into(holder.imageView);
        Glide.with(mContext).load(contact.getAvatar())
                .apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.avatar);

        if (userStarInfo.contains(Integer.valueOf(holder.tv_imageId.getText().toString())))
            Glide.with(mContext).load(R.drawable.shoucang).into(holder.iv_start);
        else
            Glide.with(mContext).load(R.drawable.shoucang_01).into(holder.iv_start);

        // 将当前列表项视图返回
        return convertView;
    }

    /**
     * 收藏按钮监听接口
     */
    public interface onItemStar{ void onItemStar(int i) throws IOException;}

    private onItemStar onItemStar;

    public void setOnItemStar(onItemStar onItemStar) {
        this.onItemStar = onItemStar;
    }

}
